﻿using NLog;
using NLog.Config;
using NLog.Targets;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JXBaseInfo
{
    public class JXControlLog
    {
        private static Hashtable m_ProcedureInfoTips = new Hashtable();
        private static LoggingConfiguration config = null;
        private static LogFactory logFactory = new LogFactory();

        static JXControlLog()
        {
            config = new LoggingConfiguration();
            logFactory.Configuration = config;
        }
        public static void InitJxLogger(string jx)
        {
            lock (logFactory)
            {
                InitLogger(jx);

            }
        }
        /// <summary>
        /// 初始化日志
        /// </summary>
        private static void InitLogger(string layer)
        {
            if (config != null)
            {
                if (config.LoggingRules.Any(t => t.LoggerNamePattern == layer) == false)
                {
                    FileTarget fileTarget = new FileTarget
                    {
                        Name = layer
                    };
                    config.AddTarget(fileTarget.Name, fileTarget);
                    fileTarget.FileName = @"${basedir}/log/jxlog/" + layer + @"/[${date:format=yyyy\-MM\-dd}].log";
                    fileTarget.Layout = "${message}";
                    fileTarget.ArchiveAboveSize = 512000;
                    fileTarget.ArchiveEvery = FileArchivePeriod.Day;
                    fileTarget.MaxArchiveFiles = 100;
                    fileTarget.ArchiveFileName = @"${basedir}/log/jxlog/" + layer + @"/[${date:format=yyyy\-MM\-dd}]{##}.log";

                    LoggingRule rule = new LoggingRule(fileTarget.Name, LogLevel.Info, fileTarget);
                    config.LoggingRules.Add(rule);
                }
            }
            logFactory.Configuration = config;
        }
        /// <summary>
        /// 将结构化的数据信息转换为规格化串
        /// </summary>
        /// <param name="Jxa"></param>
        /// <param name="Jxb"></param>
        /// <param name="Jxc"></param>
        /// <returns></returns>
        public static String FormatCabinetLayer(int Jxa, int Jxb, int Jxc)
        {
            return String.Format("{0:D2}-{1:D2}-{2:D2}", Jxa, Jxb, Jxc);
        }
        /// <summary>
        /// 插入托盘日志
        /// </summary>
        public static void InsertProdureInfo(Int32 jxa, Int32 jxb, Int32 jxc, InfoControlType infoType, String infoDesc,bool repeatFlag = false)
        {
            //InsertProdureInfo(infoType,infoDesc); 
            String key = FormatCabinetLayer(jxa, jxb, jxc);
            lock (m_ProcedureInfoTips.SyncRoot)
            {
                List<ProcedureInfo> procedureInfoList = null;
                if (m_ProcedureInfoTips.Contains(key))
                {
                    procedureInfoList = (m_ProcedureInfoTips[key] as List<ProcedureInfo>);
                }
                else
                {
                    procedureInfoList = new List<ProcedureInfo>();
                    m_ProcedureInfoTips.Add(key, procedureInfoList);
                }
                if (procedureInfoList.Count > 0 && String.Compare(procedureInfoList.LastOrDefault().InfoDesc, infoDesc) == 0 && !repeatFlag)
                    return;
                string info = string.Format("位置：{0}", infoDesc);
                var infoTips = new ProcedureInfo(infoType, infoDesc);
                string infoTypeName = "信息";
                if (infoTips.InfoType == InfoControlType.Error)
                    infoTypeName = "错误";
                procedureInfoList.Add(infoTips);
                if (procedureInfoList.Count > 300)
                {
                    procedureInfoList.RemoveRange(0, 100);
                }
            }
        }   
    }
}
